package com.sphd.emanu.activity.generalaffairs;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.staff.StaffActivity;
import com.sphd.emanu.adapter.StaffAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.utils.PermissionConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * todo 总务 主界面
 *
 * @Author Hu
 * @Time 2017/7/25 -- 15:54.
 * @Email 87683202@qq.com
 */
public class GeneralaffairsActivity extends MVPBaseActivity<GeneralaffairsContract.View, GeneralaffairsPresenter> implements GeneralaffairsContract.View {

    @BindView(R.id.add_sa)
    ImageView addSa;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    User user;
    StaffAdapter staffAdapter;
    List<Moudle> moudles;
    boolean isCanDo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generalaffairs);
        ButterKnife.bind(this);
        initView();
        initAdapter();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        mySwf.setOnRefreshListener(() -> addData());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                User userstaff = (User) adapter.getItem(position);
                bundle.putSerializable(Constant.USER_STAFF, userstaff);
                bundle.putInt(Constant.TYPE, 1);
                intent.putExtras(bundle);
                intent.setClass(context, StaffActivity.class);
                startActivityForResult(intent,Constant.REQUEST_CODE);
            }
        });
    }

    private void initAdapter() {
        setRefresh(mySwf);
        setRecy(myRecycler);
        staffAdapter = new StaffAdapter(R.layout.item_staff_general_affairs, new ArrayList<User>());
        staffAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(staffAdapter);
    }

    private void initView() {
        setTitleBar(toolbar, context.getString(R.string.title_activity_generalaffairs));
        intent = getIntent();
        bundle = intent.getExtras();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_ZONGWU_13);
        if (!isCanDo){
            addSa.setVisibility(View.GONE);
        }
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.selectUser(user.getOid());
    }

    @OnClick(R.id.add_sa)
    public void onViewClicked() {
        bundle.putInt(Constant.TYPE, 0);
        intent.putExtras(bundle);
        intent.setClass(context, StaffActivity.class);
        startActivityForResult(intent,Constant.REQUEST_CODE);
    }

    @Override
    public void setListBean(List<User> userList) {
        dissProgressDialog();
        staffAdapter.setNewData(userList);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Constant.REQUEST_CODE){
            addData();
        }
    }
}
